#include "syscall.h"


int
main() {
    SpaceId newProc;
    OpenFileId input = ConsoleInput;
    OpenFileId output = ConsoleOutput;
    char prompt[2], ch, buffer[60];
    int i;
    char *argsBuffer[20];

    int argsCount = 0;
    int j;
    int k;

    prompt[0] = '-';
    prompt[1] = '-';


    while( 1 ) {
        Write(prompt, 2, output);

        i = 0;

        do {

            Read(&buffer[i], 1, input);

        } while( buffer[i++] != '\n' );

        buffer[--i] = '\0';

        j = 0;
        k = 0;

        Write("Fail\n",6,output);
        Write(buffer, 10, output);

        argsBuffer[0] = buffer;
        argsCount ++;

        //Read(&pause, 1, input);

        while ( buffer[j] != '\0' ) {
            //Write("Fail\n",6,output);

            if ( buffer[j] == ' ' ) {
                buffer[j] = '\0';
                argsBuffer[argsCount] = &(buffer[j + 1]);
                argsCount ++;

                //k = 0;
                //argsCount++;
                //argsBuffer[argsCount] = 0; // Allocate space
            }

            //else
            //{
            //	argsBuffer[argsCount][k] = buffer[j];
            //	k++;
            //}
            //
            j++;
            //Write("here",4,output);
        }

        Write("out of loop\n",12,output);
        //Read(&pause, 1, input);
        if( i > 0 ) {
            Write("\n\n\nOUTPUT:\n",11, output);
            //Write(argsBuffer, 15 ,output);
            //Write("\n\n\n",3,output);

            char *actualArgv[argsCount];
            int m;
            for(m= 0; m < argsCount - 1; m++) {
                actualArgv[m] = argsBuffer[m + 1];
            }

            actualArgv[argsCount - 1] = 0;


            newProc = Exec( argsBuffer[0] , actualArgv);

            if( newProc != -1) {
                Write("newProc success\n", 16, output);
                Join(newProc);
            } else {
                Write("Fail to exec\n", 13, output);
            }

        } else {
            Write("Fail to make exec call\n", 23, output);
        }
    }
}

